/**
 * 城市默认分类系数编辑
 * @author linfeng
 * @since 2019/08/24 16:06
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let laydate = layui.laydate;

    let effectiveDateObj = {
        elem: '#effectiveDate',
        type: 'date',
        btns: ['clear', 'confirm'],
        min: 1
    };

    laydate.render(effectiveDateObj);

    form.render(null, "zmn-form");

    Date.prototype.format = function(fmt) {
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        if (!valueVerify(obj.field.ratio)){
            return false;
        }
        if (!valueVerify(obj.field.meterRatio)){
            return false;
        }
        var openId = layer.open({type: 3});
        let data = {};
        data["addFlag"] = obj.field.addFlag;
        data["channelId"] = obj.field.channelId;
        data["brandId"] = obj.field.brandId;
        data["priceType"] = obj.field.priceType;
        data["bizType"] = obj.field.bizType;
        data["toBeEffectiveRatio"] = obj.field.ratio;
        data["meterToBeEffectiveRatio"] = obj.field.meterRatio;
        if (obj.field.effectiveDate == null || obj.field.effectiveDate == '' || obj.field.effectiveDate == undefined) {
            var nowDate = new Date();
            let effectiveDateStr = new Date(nowDate.getTime() + 24*60*60*1000).format("yyyy-MM-dd");
            data['effectiveDate'] = effectiveDateStr;
        } else {
            data['effectiveDate'] = obj.field.effectiveDate;
        }

        var url = "/channel/brand/ratio/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(json) {
                layer.close(openId);
                layer.msg(json.message);
                setTimeout(()=>{
                    if (json.status == 200) {
                        zmnLayUtil.closeThisIframeMsg(parent, json.message, "zmn-table");
                        parent.location.reload();
                    }
                },1000)
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    //对外暴露的接口
    exports('channel_brandratio_edit', {});
});

function valueVerify(value) {
    var reg=/^\d+(\.\d{1,2})?$/;

    if (value == ""){
        return true;
    }

    if (isNaN(value)){
        layer.alert("请输入正确的数字");
        return false;
    }

    // if (value == '0' || value == '0.0' || value == '0.00'){
    //     return true;
    // }

    if(!reg.test(value))
    {
        layer.alert("请输入整数或者1到2位小数");
        return false;
    }

    if ((parseFloat(0.6) <= parseFloat(value) && parseFloat(3.0) >= parseFloat(value))){
    } else{
        layer.alert("系数值不符合规定,系数值范围0.6~3.0");
        return false;
    }

    return true;
}

function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}
